package com.wuercloud.service.impl;

import org.springframework.amqp.rabbit.core.RabbitManagementTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wuercloud.dao.ConfigDao;
import com.wuercloud.entity.ConfigEntity;
import com.wuercloud.service.ConfigService;
import com.wuercloud.swagger.WawaRabbitClient;



@Service("configService")
public class ConfigServiceImpl implements ConfigService {
	@Autowired
	private ConfigDao configDao;
	@Value("${rabbitmq-VirtualHost}")
	private String vhost;
	@Autowired
	private RabbitManagementTemplate rabbitManagementTemplate;
	@Autowired
	private WawaRabbitClient wawaRabbitClient;

	
	@Override
	public ConfigEntity queryObject(Integer configId){
		return configDao.queryObject(configId);
	}
	
	@Override
	public List<ConfigEntity> queryList(Map<String, Object> map){
		return configDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return configDao.queryTotal(map);
	}
	
	@Override
	public void save(ConfigEntity config){
		rabbitManagementTemplate.getClient().bindQueue(vhost, config.getQueuesName(), config.getExchangesName(),config.getBindingKey() );
		configDao.save(config);
	}
	
	@Override
	public void update(ConfigEntity config){
		configDao.update(config);
	}
	
	@Override
	public void delete(Integer configId){
		configDao.delete(configId);
	}
	
	@Override
	public void deleteBatch(Integer[] configIds){
		
		Arrays.asList(configIds).stream().forEach(configId -> {
			ConfigEntity configEntity = configDao.queryObject(configId);
			
			wawaRabbitClient.UnBindQueue(vhost, configEntity.getQueuesName(), configEntity.getExchangesName(),configEntity.getBindingKey());
		});
		configDao.deleteBatch(configIds);
	}
	
}
